package com.deprice4u_client;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

public class DePrice4U_clienetActivity extends Activity {

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// User ��낫 媛����� ����댁� SharedPreference���ｋ� Thread
		Thread userInformationDownloadthread = new Thread() {
			@Override
			public void run() {
				String url = Deprice.SERVER_URL+"usermanager?action=getentry&loginid=soonsm";
				String userId = null;
				String email = null;
				String deposit = null;
				try {
					URL queryURL = new URL(url);
					DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
							.newInstance();
					DocumentBuilder docBuilder = docBuilderFactory
							.newDocumentBuilder();
					Document doc = docBuilder.parse(queryURL.openStream());
					NodeList items = doc.getElementsByTagName("user");
					Node item = items.item(0);

					userId = ((Element) item).getElementsByTagName("userid")
							.item(0).getChildNodes().item(0).getNodeValue()
							.trim();
					email = ((Element) item).getElementsByTagName("email")
							.item(0).getChildNodes().item(0).getNodeValue()
							.trim();
					deposit = ((Element) item).getElementsByTagName("deposit")
							.item(0).getChildNodes().item(0).getNodeValue()
							.trim();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				SharedPreferences settings = getSharedPreferences(
						Preference.PREFS_NAME, 0);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString(Preference.USER_ID, userId);
				editor.putString(Preference.EMAIL, email);
				editor.putInt(Preference.DEPOSIT, Integer.valueOf(deposit));
				editor.commit();
			}
		};
		userInformationDownloadthread.start();

		Thread splashThread = new Thread() {
			public void run() {
				try {
					int waited = 0;
					while (waited < 1000) {
						sleep(100);
						waited += 100;
					}
				} catch (InterruptedException e) {

				} finally {
					finish();
					Intent i = new Intent();
					
					// For MakeNewRequestOfProduct Testing
					// i.setClassName("com.deprice4u_client", "com.deprice4u_client.BackgroundActivity");
					// i.setClassName("com.deprice4u_client", "com.deprice4u_client.MakeNewRequestOfProduct");
					i.setClassName("com.deprice4u_client", "com.deprice4u_client.MakeNewRequestWithNaver");
					
					startActivity(i);
				}
			}
		};
		splashThread.start();
		/*
		 * Intent it = new Intent(this, BackgroundActivity.class);
		 * startActivity(it);
		 */
	}
}